﻿using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Data.SqlClient;

namespace zyconfig
{
    /// <summary>
    /// 本对象用与提供对OLEDB数据库的常用访问操作。
    /// </summary>
    public class OleDbOperHandler : DbOperHandler
    {
        /// <summary>
        /// 构造函数，接收一个OLEDB数据库连接对象OleDbConnection
        /// </summary>
        /// <param name="_conn"></param>
        public OleDbOperHandler(System.Data.OleDb.OleDbConnection _conn,string db)
        {
            conn = _conn;
            dbType = DatabaseType.OleDb;
            if (string.IsNullOrEmpty(db))
                odbType = DatabaseType.MSSQL;
            else
                odbType = DatabaseType.ORACLE;
            conn.Open();
            cmd = conn.CreateCommand();
            da = new System.Data.OleDb.OleDbDataAdapter();
        }

        public OleDbOperHandler(SqlConnection _conn)
        {
            conn = _conn;
            dbType = DatabaseType.MSSQL;
            conn.Open();
            cmd = conn.CreateCommand();
            da = new SqlDataAdapter();
        }

        public OleDbOperHandler(MySqlConnection _conn)
        {
            conn = _conn;
            dbType = DatabaseType.MYSQL;
            conn.Open();
            cmd = conn.CreateCommand();
            da = new MySqlDataAdapter();
        }

        public OleDbOperHandler(OracleConnection _conn)
        {
            conn = _conn;
            dbType = DatabaseType.ORACLE;
            odbType = DatabaseType.ORACLE;
            conn.Open();
            cmd = conn.CreateCommand();
            da = new OracleDataAdapter();
        }

        /// <summary>
        /// 构造函数，接收一个mdb文件
        /// </summary>
        /// <param name="path"></param>
        public OleDbOperHandler(string path)
        {
            conn = new System.Data.OleDb.OleDbConnection(path);
            dbType = DatabaseType.OleDb;
            conn.Open();
            cmd = conn.CreateCommand();
            da = new System.Data.OleDb.OleDbDataAdapter();
        }
        /// <summary>
        /// 产生OleDbCommand对象所需的查询参数。
        /// </summary>
        protected override void GenParameters()
        {
            if(dbType== DatabaseType.ORACLE)
            {
                OracleCommand oleCmd = (OracleCommand)cmd;
                if (this.alFieldItems.Count > 0)
                {
                    for (int i = 0; i < alFieldItems.Count; i++)
                    {
                        oleCmd.Parameters.Add("@para" + i.ToString(), ((DbKeyItem)alFieldItems[i]).fieldValue.ToString());
                    }
                }

                if (this.alSqlCmdParameters.Count > 0)
                {
                    for (int i = 0; i < this.alSqlCmdParameters.Count; i++)
                    {
                        oleCmd.Parameters.Add("@spara" + i.ToString(), ((DbKeyItem)alSqlCmdParameters[i]).fieldValue.ToString());
                    }
                }
                if (this.alConditionParameters.Count > 0)
                {
                    for (int i = 0; i < this.alConditionParameters.Count; i++)
                    {
                        oleCmd.Parameters.Add("@cpara" + i.ToString(), ((DbKeyItem)alConditionParameters[i]).fieldValue.ToString());
                    }
                }
            }
            else if (dbType == DatabaseType.MYSQL)
            {
                MySqlCommand oleCmd = (MySqlCommand)cmd;
                if (this.alFieldItems.Count > 0)
                {
                    for (int i = 0; i < alFieldItems.Count; i++)
                    {
                        oleCmd.Parameters.AddWithValue("@para" + i.ToString(), ((DbKeyItem)alFieldItems[i]).fieldValue.ToString());
                    }
                }

                if (this.alSqlCmdParameters.Count > 0)
                {
                    for (int i = 0; i < this.alSqlCmdParameters.Count; i++)
                    {
                        oleCmd.Parameters.AddWithValue("@spara" + i.ToString(), ((DbKeyItem)alSqlCmdParameters[i]).fieldValue.ToString());
                    }
                }
                if (this.alConditionParameters.Count > 0)
                {
                    for (int i = 0; i < this.alConditionParameters.Count; i++)
                    {
                        oleCmd.Parameters.AddWithValue("@cpara" + i.ToString(), ((DbKeyItem)alConditionParameters[i]).fieldValue.ToString());
                    }
                }
            }
            else
            {
                System.Data.OleDb.OleDbCommand oleCmd = (System.Data.OleDb.OleDbCommand)cmd;
                if (this.alFieldItems.Count > 0)
                {
                    for (int i = 0; i < alFieldItems.Count; i++)
                    {
                        oleCmd.Parameters.AddWithValue("@para" + i.ToString(), ((DbKeyItem)alFieldItems[i]).fieldValue.ToString());
                    }
                }

                if (this.alSqlCmdParameters.Count > 0)
                {
                    for (int i = 0; i < this.alSqlCmdParameters.Count; i++)
                    {
                        oleCmd.Parameters.AddWithValue("@spara" + i.ToString(), ((DbKeyItem)alSqlCmdParameters[i]).fieldValue.ToString());
                    }
                }
                if (this.alConditionParameters.Count > 0)
                {
                    for (int i = 0; i < this.alConditionParameters.Count; i++)
                    {
                        oleCmd.Parameters.AddWithValue("@cpara" + i.ToString(), ((DbKeyItem)alConditionParameters[i]).fieldValue.ToString());
                    }
                }
            }
            
        }
    }
}
